import express from 'express'
import cors from 'cors'
import { createServer } from "http"
import { expressjwt as expressJWT} from 'express-jwt'
import { TOKEN_KEY, TIME } from './config/jwt/index.mjs'
import handleRes from './utils/handleRes.mjs'
import router from './router/index.mjs'
import { handleSocket, corsOptions } from './utils/handleSocket.mjs'

const app = express()
const httpServer = createServer(app)

app.use(cors(corsOptions))
app.use(express.urlencoded({ extended: false }))
app.use(expressJWT({ secret: TOKEN_KEY, algorithms: ['HS256'] }).unless({ 
    path: [/\/api\/out\/*/]
}))
app.use(handleRes.operationUnauthorized)
app.use(handleRes.operationFailed)
app.use(handleRes.operationSuccess)
app.use('/api', router)

handleSocket(httpServer)

httpServer.listen(3000, function () {
    console.log('api server running at http://127.0.0.1:3000')
})
